window.onload = function () {
	var appId = 'wxf364cbeb5d4862f2'; // 公众号的appid
	var userinfoURL = "https://gh5api.mp.sztv.com.cn/huanbao/common/getwxInfo"; //  解析用户的信息的后端接口（获取完会存在localStorage， key为userWX）
	// 获取url链接的参数
	var $_GET = (function () {
		var url = window.document.location.href.toString();
		var u = url.split("?");
		if (typeof (u[1]) == "string") {
			u = u[1].split("&");
			var get = {};
			for (var i in u) {
				var j = u[i].split("=");
				get[j[0]] = decodeURIComponent(j[1]);
			}
			return get;
		} else {v
			return {};
		}
	})();

	//判断是否是微信浏览器的函数
	function isWeiXin() {
		//window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
		var ua = window.navigator.userAgent.toLowerCase();
		//通过正则表达式匹配ua中是否含有MicroMessenger字符串
		if (ua.match(/MicroMessenger/i) == 'micromessenger') {
			return true;
		} else {
			return false;
		}
	}

	//获取用户信息
	if (isWeiXin()) {
		var localVia = localStorage.getItem('userWX');
		var windowURL = window.location.href;
		windowURL = encodeURIComponent(windowURL);
		var code = $_GET['code'];
		var from = $_GET['from'];

		//如果本地的用户信息不存在
		if (localVia == null) {
			if (!code) {
				var scope = "snsapi_userinfo";
				var redirectURL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + windowURL + "&response_type=code&scope=" + scope + "&state=STATE#wechat_redirect";
				window.location.href = redirectURL;
				return;
			}
			if (code) {
				var userData = {
					"code": code,
				};
				$.post(userinfoURL, userData, function (response) {
					var userInfo = JSON.stringify(response.obj);
					//将用户名存储在本地
					localStorage.setItem('userWX', userInfo);
				}
				);
			}
		}
	}
}